package com.skyline.jfs.namespace.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.skyline.jfs.namespace.model.JFSDirectory;
import com.skyline.jfs.namespace.model.JFSLsResult;
import com.skyline.jfs.namespace.service.JFSDirectoryService;
import com.skyline.jfs.namespace.service.JFSFileService;

@Controller
@RequestMapping(value = "/namespace/")
public class JFSDirectoryController {

	@Autowired
	private JFSDirectoryService jfsDirectoryService;
	
	@Autowired 
	private JFSFileService jfsFileSerivce;

	@RequestMapping(value = "ls/{id}.html", method = RequestMethod.GET)
	public String ls(HttpSession session, HttpServletRequest request,
			@PathVariable Long id) {
		if (session.getAttribute("administrator") != null) {
			JFSLsResult lsResult = jfsDirectoryService.ls(id);
			request.setAttribute("lsResult", lsResult);
			return "namespace/ls";
		} else {
			return "admin/login";
		}
	}

	@RequestMapping(value = "createDir/{id}.html", method = RequestMethod.GET)
	public String createDirRequest(HttpSession session,
			HttpServletRequest request, @PathVariable Long id) {
		if (session.getAttribute("administrator") != null) {
			JFSDirectory parentDir = jfsDirectoryService.get(id);
			request.setAttribute("parentDir", parentDir);
			return "namespace/createDir";
		} else {
			return "admin/login";
		}
	}

	@RequestMapping(value = "createDir.html", method = RequestMethod.POST)
	public String createDirExecute(HttpSession session,
			HttpServletRequest request, Long parent, String parentPathHidden,
			String name, Integer permission) {
		if (session.getAttribute("administrator") != null) {
			jfsDirectoryService.createDir(parent, parentPathHidden+name+"/", name, permission);
			return "redirect:/namespace/ls/"+parent+".html";
		} else {
			return "admin/login";
		}
	}
	
	@RequestMapping(value = "modifyDir/{id}.html", method = RequestMethod.GET)
	public String modifyDirRequest(HttpSession session,
			HttpServletRequest request, @PathVariable Long id) {
		if (session.getAttribute("administrator") != null) {
			JFSDirectory currentDir = jfsDirectoryService.get(id);
			request.setAttribute("currentDir", currentDir);
			return "namespace/modifyDir";
		} else {
			return "admin/login";
		}
	}
	
	@RequestMapping(value = "modifyDir.html", method = RequestMethod.POST)
	public String modifyDirExecute(HttpSession session,
			HttpServletRequest request, Long id,Long parent,String name,Integer permission) {
		if (session.getAttribute("administrator") != null) {
			JFSDirectory currentDir = jfsDirectoryService.get(id);
			return "redirect:/namespace/ls/"+parent+".html";
		} else {
			return "admin/login";
		}
	}
	
	@RequestMapping(value="ls.html",method=RequestMethod.GET)
	public String ls(HttpSession session,HttpServletRequest request,String currentDir){
		if (session.getAttribute("administrator") != null) {
			//System.out.println(currentDir);
			JFSLsResult lsResult = jfsDirectoryService.ls(currentDir);//ls();
			request.setAttribute("lsResult", lsResult);
			return "namespace/ls";
		} else {
			return "admin/login";
		}
	}
}
